package com.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

@Component
public class HqlDAO  {



    @PersistenceContext
    private  EntityManager  em;

    public List executePage(String hql, Integer page, Integer size , Object ...p)
    {
       Query query = em.createQuery(hql);
       if (p!=null)
       {
           for (int i=0;i<p.length;i++)
           {
               query.setParameter(i,p[i]);
           }
       }
       if (page!=null && size!=null && page>0 && size>0)
       {
           query.setFirstResult((page-1)*size).setMaxResults(size);
       }

       return query.getResultList();


    }
}
